#include <stdio.h>
#include <stdlib.h>

#include <string>

#include "xpression-drawer/ExpressionDrawer.h"
#include "xpression-drawer/ExpressionMesh.h"

#include "xpression-model/Model.h"
#include "xpression-model/Expression.h"
#include "xpression-model/Variable.h"

using namespace std;

// C/C++ Interaction
extern "C"
{
	extern int yyparse();
	extern FILE* yyin;
}

#define WINDOW_WIDTH  640
#define WINDOW_HEIGHT 480

int main(int argc, char *argv[])
{
	if(argc < 2)
	{
		fprintf(stderr,
				"%s\n",
				"Argument Error: please provide a sequence file.");

		// Exiting
		exit(EXIT_FAILURE);
	}

	yyin = fopen(argv[1], "r");
	if(yyin == NULL)
	{
		fprintf(stderr,
				"%s\n",
				"Nothing file parse. Please provide a file contained in xpression-sequences or create new ones");

		// Exiting
		exit(EXIT_FAILURE);
	}

	if(yyparse())
	{
		fprintf(stderr,
				"%s\n",
				"An error occurred while attempting to parse sequence. Please provide a valid equation sequence.");

		// Exiting
		exit(EXIT_FAILURE);
	}
	else
	{
		// Closing File
		fclose(yyin);
	}

	fprintf(stdout, "%s\n", "Sequence properly parsed. Building 3D Scene...");

	// Building Volume Expression
	Expression* expressionTreeBase = (Expression*) getTreeBase();
	Variable* phi = (Variable*) getPhi();
	Variable* theta = (Variable*) getTheta();

	unsigned int phiStep = getPhiStep();
	unsigned int thetaStep = getThetaStep();

	// Drawing
	ExpressionDrawer* drawer = new ExpressionDrawer(&argc, argv, WINDOW_WIDTH, WINDOW_HEIGHT);
	ExpressionMesh* mesh = new ExpressionMesh(expressionTreeBase, phi, theta, phiStep, thetaStep);
	drawer->AddMesh(mesh);
	drawer->InitOpenGL();

	return 0;
}
